<?php

namespace App\Containers\AppSection\Qualification\UI\API\Requests;

use App\Containers\AppSection\Qualification\Tasks\FindQualificationByIdTask;
use App\Ship\Parents\Requests\Request;

class GetQualificationRequest extends Request
{
    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => 'admin|moderator|investor|admin',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [
        // 'id',
    ];

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            // 'id' => 'required'
        ];
    }

    public function isIndiv(): bool
    {
        if ($this->profile()->user->hasRole('admin|moderator')) {
            return true;
        }

        return $this->profile()->hasLegalForm('indiv');
    }

    public function isOwner(): bool
    {
        if ($this->profile()->user->hasRole('admin|moderator')) {
            return true;
        }

        $qualification = app(FindQualificationByIdTask::class)->run($this->id);
        return $qualification->profile->id === $this->profile()->id;
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
            'isIndiv',
            'isOwner'
        ]);
    }
}
